package com.sixmac.controller.backend;

import com.sixmac.common.DataTableFactory;
import com.sixmac.controller.common.CommonController;
import com.sixmac.entity.Account;
import com.sixmac.service.AccountService;
import com.sixmac.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Created by Administrator on 2016/3/14 0014.
 */
@Controller
@RequestMapping(value = "backend/account")
public class AccountController extends CommonController {

    @Autowired
    private AccountService accountService;

    @RequestMapping("index")
    public String index() {
        return "backend/账户列表";
    }

    @RequestMapping("/list")
    public void list(HttpServletResponse response,
                     String num,
                     String mobile,
                     String name,
                     Integer draw,
                     Integer start,
                     Integer length) {
        if (null == start || start == 0) {
            start = 1;
        }
        int pageNum = getPageNum(start, length);
        Page<Account> page = accountService.page(num, mobile, name, pageNum, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, page);
        WebUtil.printJson(response, result);
    }

    @RequestMapping("show")
    public String show(ModelMap model, Integer id) {
        if (null != id) {
            Account account = accountService.getById(id);
            account.setAppUserList(accountService.findAppUserListByAccountId(id));
            account.setWxUserList(accountService.findWxUserListByAccountId(id));
            model.addAttribute("account", account);
        }

        return "backend/账户详情";
    }
}
